#pragma once
#include<sys/epoll.h>
#include"log.hpp"
#include<string>
#include"nocopy.hpp"
int timeout=3000;
extern Log lg;
//epoll的接口进行封装

class Epoll:public nocopy
{
    public:
    Epoll()
    {
        //epoll模型创建，创建整个结构（二叉树、函数回调、就绪队列）
        epoll_model=epoll_create(128);
        if(epoll_model<0)
        {
        lg(FATAL,"Epoll Create fail!! errstring is:%s",strerror(errno));
       exit(1);
        }
    }
    int EpollWait(struct epoll_event events[],int nums)
    {
        //等待方法，从就绪队列里捞已就绪fd、事件
       int n= epoll_wait(epoll_model,events,nums,timeout);
       return n;
    }
    int EpollUpdata(int oper,int sock,uint32_t event)//什么操作、哪个套接字、关心什么事件
    {
        //控制方法，更新红黑树
        int n;
        if(oper==EPOLL_CTL_DEL)
        {
          n=epoll_ctl(epoll_model,oper,sock,nullptr);
          if(n!=0)
          {
            lg(ERROR,"epoll_ctl delete error!!");
          }
        }
        else{
          struct epoll_event ev;
        ev.events=event;
        ev.data.fd=sock;//为了方便以后知道是哪个sock
      n=  epoll_ctl(epoll_model,oper,sock,&ev);
      if(n!=0)
      {
        lg(WARNING,"EopllUpata fail!! errstring is:%s",strerror(errno));
      }
        }
        return n;
    }
    ~Epoll()
    {
        if(epoll_model>=0)
        close(epoll_model);
    }
    private:
    int epoll_model;//epoll模型（create的返回值）
};
